<template>
  <div class="statistics-overview">
    <!-- 统计卡片 -->
    <el-row :gutter="20">
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card class="statistics-card" shadow="hover">
          <div class="card-content">
            <div class="card-icon" :style="{ backgroundColor: card.color }">
              <el-icon>
                <component :is="card.icon" />
              </el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">{{ card.title }}</div>
              <div class="card-value">
                <span class="number">{{ card.value }}</span>
                <span class="unit">{{ card.unit }}</span>
              </div>
              <div class="card-trend">
                <span :class="['trend', card.trend > 0 ? 'up' : 'down']">
                  {{ Math.abs(card.trend) }}%
                  <el-icon>
                    <CaretTop v-if="card.trend > 0" />
                    <CaretBottom v-else />
                  </el-icon>
                </span>
                <span class="trend-text">较上月</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 营收趋势图 -->
      <el-col :span="16">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>营收趋势</span>
              <el-radio-group v-model="revenueTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="revenueChartRef"></div>
        </el-card>
      </el-col>

      <!-- 学员来源分布 -->
      <el-col :span="8">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>学员来源分布</span>
            </div>
          </template>
          <div class="chart-container" ref="sourceChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 教练排行榜 -->
      <el-col :span="12">
        <el-card class="ranking-card">
          <template #header>
            <div class="card-header">
              <span>教练排行榜</span>
              <el-select v-model="coachRankingType" size="small">
                <el-option label="学员数量" value="student_count" />
                <el-option label="通过率" value="pass_rate" />
                <el-option label="评分" value="rating" />
              </el-select>
            </div>
          </template>
          <div class="ranking-list">
            <div
              v-for="(item, index) in coachRankingList"
              :key="item.name"
              class="ranking-item"
            >
              <div class="ranking-index" :class="{ top3: index < 3 }">
                {{ index + 1 }}
              </div>
              <el-avatar :size="40" :src="item.avatar" />
              <div class="ranking-info">
                <div class="name">{{ item.name }}</div>
                <div class="value">{{ item.value }}{{ item.unit }}</div>
              </div>
              <div class="ranking-trend">
                <el-tag :type="item.trend >= 0 ? 'success' : 'danger'" size="small">
                  {{ item.trend >= 0 ? '+' : '' }}{{ item.trend }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 培训项目统计 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>培训项目统计</span>
              <el-tabs v-model="trainingTab" class="header-tabs" @tab-change="handleTrainingTabChange">
                <el-tab-pane label="报名人数" name="enrollment" />
                <el-tab-pane label="完成率" name="completion" />
              </el-tabs>
            </div>
          </template>
          <div class="chart-container" ref="trainingChartRef"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import {
  User,
  Money,
  Opportunity,
  DataLine,
  CaretTop,
  CaretBottom
} from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 统计卡片数据
const statisticsCards = [
  {
    title: '总收入',
    value: '268,550',
    unit: '元',
    icon: Money,
    color: '#409EFF',
    trend: 12.5
  },
  {
    title: '学员总数',
    value: '1,234',
    unit: '人',
    icon: User,
    color: '#67C23A',
    trend: 8.2
  },
  {
    title: '报名人数',
    value: '156',
    unit: '人',
    icon: Opportunity,
    color: '#E6A23C',
    trend: -3.5
  },
  {
    title: '通过率',
    value: '92.5',
    unit: '%',
    icon: DataLine,
    color: '#F56C6C',
    trend: 2.8
  }
]

// 图表相关
const revenueTimeRange = ref('month')
const coachRankingType = ref('student_count')
const trainingTab = ref('enrollment')

const revenueChartRef = ref(null)
const sourceChartRef = ref(null)
const trainingChartRef = ref(null)

let revenueChart = null
let sourceChart = null
let trainingChart = null

// 教练排行榜数据
const coachRankingList = ref([
  {
    name: '李教练',
    avatar: 'https://placeholder.com/40',
    value: 128,
    unit: '人',
    trend: 5
  },
  {
    name: '王教练',
    avatar: 'https://placeholder.com/40',
    value: 115,
    unit: '人',
    trend: 3
  },
  {
    name: '张教练',
    avatar: 'https://placeholder.com/40',
    value: 98,
    unit: '人',
    trend: -2
  },
  {
    name: '赵教练',
    avatar: 'https://placeholder.com/40',
    value: 89,
    unit: '人',
    trend: 4
  },
  {
    name: '孙教练',
    avatar: 'https://placeholder.com/40',
    value: 76,
    unit: '人',
    trend: 1
  }
])

// 初始化营收趋势图
const initRevenueChart = () => {
  revenueChart = echarts.init(revenueChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['总收入', '报名收入', '其他收入']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
    },
    yAxis: {
      type: 'value',
      name: '金额（元）'
    },
    series: [
      {
        name: '总收入',
        type: 'line',
        smooth: true,
        data: [150000, 180000, 220000, 190000, 250000, 268000, 280000]
      },
      {
        name: '报名收入',
        type: 'line',
        smooth: true,
        data: [120000, 150000, 180000, 160000, 210000, 230000, 240000]
      },
      {
        name: '其他收入',
        type: 'line',
        smooth: true,
        data: [30000, 30000, 40000, 30000, 40000, 38000, 40000]
      }
    ]
  }
  revenueChart.setOption(option)
}

// 初始化来源分布图
const initSourceChart = () => {
  sourceChart = echarts.init(sourceChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '学员来源',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 45, name: '线上推广' },
          { value: 25, name: '老学员推荐' },
          { value: 20, name: '门店咨询' },
          { value: 10, name: '其他渠道' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  sourceChart.setOption(option)
}

// 初始化培训项目统计图
const initTrainingChart = () => {
  trainingChart = echarts.init(trainingChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['C1自动档', 'C1手动档', 'C2手动档', 'B2货车']
    },
    yAxis: {
      type: 'value',
      name: trainingTab.value === 'enrollment' ? '人数' : '完成率（%）'
    },
    series: [
      {
        name: trainingTab.value === 'enrollment' ? '报名人数' : '完成率',
        type: 'bar',
        barWidth: '60%',
        data: trainingTab.value === 'enrollment'
          ? [52, 34, 28, 12]
          : [95, 88, 82, 90]
      }
    ]
  }
  trainingChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  revenueChart?.resize()
  sourceChart?.resize()
  trainingChart?.resize()
}

// 培训项目统计切换
const handleTrainingTabChange = () => {
  initTrainingChart()
}

onMounted(() => {
  // 初始化图表
  initRevenueChart()
  initSourceChart()
  initTrainingChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  revenueChart?.dispose()
  sourceChart?.dispose()
  trainingChart?.dispose()
})
</script>

<style scoped>
.statistics-overview {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.card-icon :deep(.el-icon) {
  font-size: 24px;
}

.card-info {
  flex: 1;
}

.card-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.card-value {
  margin-bottom: 8px;
}

.card-value .number {
  font-size: 24px;
  font-weight: bold;
  margin-right: 4px;
}

.card-value .unit {
  font-size: 14px;
  color: #909399;
}

.card-trend {
  font-size: 12px;
}

.trend {
  display: inline-flex;
  align-items: center;
  margin-right: 4px;
}

.trend.up {
  color: #67c23a;
}

.trend.down {
  color: #f56c6c;
}

.trend-text {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-tabs :deep(.el-tabs__header) {
  margin: 0;
}

.chart-container {
  height: 320px;
}

.ranking-card {
  height: 400px;
}

.ranking-list {
  height: 320px;
  overflow-y: auto;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #ebeef5;
}

.ranking-item:last-child {
  border-bottom: none;
}

.ranking-index {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #f4f4f5;
  color: #909399;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-size: 14px;
}

.ranking-index.top3 {
  background-color: #ffd700;
  color: #fff;
}

.ranking-info {
  flex: 1;
  margin: 0 12px;
}

.ranking-info .name {
  font-size: 14px;
  margin-bottom: 4px;
}

.ranking-info .value {
  font-size: 12px;
  color: #909399;
}

.ranking-trend {
  min-width: 60px;
  text-align: right;
}
</style>